package com.xinyiweidai.xinyiweidai.module.my;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lan.mylibrary.okhttp.L;
import com.lan.mylibrary.okhttp.OkHttpUtils;
import com.lan.mylibrary.okhttp.callback.StringCallback;
import com.lan.mylibrary.ui.MyTitle;
import com.lan.mylibrary.ui.MyToast;
import com.lan.mylibrary.util.AppUtil;
import com.xinyiweidai.xinyiweidai.BuildConfig;
import com.xinyiweidai.xinyiweidai.R;
import com.xinyiweidai.xinyiweidai.base.BaseActivity;
import com.xinyiweidai.xinyiweidai.bean.AppVersionBean;
import com.xinyiweidai.xinyiweidai.dialog.DownloadDialog;
import com.xinyiweidai.xinyiweidai.dialog.UpdateDialog;
import com.xinyiweidai.xinyiweidai.util.Api;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Request;

/**
 * class:   SettingActivity
 * data：   2018/9/28  18:15
 * author:  LanX
 * Description: 设置界面
 *
 */
public class SettingActivity extends BaseActivity  implements UpdateDialog.OnUpdateDialogClickListener{


    private TextView tv_versionCode;
    private TextView tv_about_us;
    private TextView tv_check_version;
    private String url, size;
    private int enforce;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }


    @Override
    protected void initToolBar() {
        new MyTitle(this,R.drawable.ic_back,"设置",0,true);
    }


    @SuppressLint("SetTextI18n")
    @Override
    protected void initView(Bundle savedInstanceState) {

        tv_versionCode = (TextView) findViewById(R.id.tv_versionCode);
        tv_about_us = (TextView) findViewById(R.id.tv_about_us);
        tv_check_version = (TextView) findViewById(R.id.tv_check_version);
        tv_versionCode.setText("版本号v "+ BuildConfig.VERSION_NAME);
    }


    @Override
    protected void addClickEvent() {
        tv_check_version.setOnClickListener(view ->getVersion());
        tv_about_us.setOnClickListener(view -> startNewActivity(AboutActivity.class));
    }


    private void getVersion() {
        showLoading("正在加载");
        Map<String,String> map=new HashMap<>();
        map.put("version", "android");
        OkHttpUtils
                .get()
                .url(Api.updateVersion)
                .params(map)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        dismissLoading();
                        MyToast.showError(SettingActivity.this, "网络异常");
                    }

                    @Override
                    public void onResponse(String response) {
                        L.e(response);
                        dismissLoading();
                        parseData(response);
                    }
                });
    }


    private void parseData(String response) {
        AppVersionBean bean=new Gson().fromJson(response,AppVersionBean.class);
        if (bean.getCode()==1){
            String newVersion = bean.getNewversion();
            if (newVersion.equals(BuildConfig.VERSION_NAME)){
                MyToast.showSuccess(this, "当前已是最新版本");
                return;
            }
            size = bean.getPackagesize();
            url = bean.getDownloadurl();
            enforce = bean.getEnforce();
            showUpdateDialog(size);
        }
    }


    private void showUpdateDialog(String size) {
        UpdateDialog fragment = UpdateDialog.newInstance(size, enforce, this);
        fragment.show(getSupportFragmentManager(), "update_dialog");
    }


    @Override
    public void onUpdateDialogClick() {
        DownloadDialog downloadDialogFragment = DownloadDialog.newInstance(size, url);
        downloadDialogFragment.show(getFragmentManager(), "download_dialog");
    }
}
